home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / LAYOUT.PAK / LAYDIA.CPP next >
C/C++ Source or Header  |  1997-05-06  |  13KB  |  394 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1993, 1995 by Borland International, All Rights Reserved
  4. //----------------------------------------------------------------------------
  5.  
  6. //
  7. // This is an example of how to use TRACE macros in a program without
  8. // requiring an OWL diagnostic build.
  9. // Just define __TRACE (and/or __WARN) before any OWL headers
  10. // Then you can use TRACE() or WARN() directly
  11. //
  12. #define __TRACE
  13.  
  14. #include <owl/pch.h>
  15. #include <owl/applicat.h>
  16. #include <owl/layoutwi.h>
  17. #include <owl/layoutco.h>
  18. #include <owl/dialog.h>
  19. #include <owl/combobox.h>
  20. #include <owl/edit.h>
  21. #include <string.h>
  22. #include <stdio.h>
  23. #include "layout.rh"
  24. #include "laydia.h"
  25.  
  26.  
  27. DEFINE_RESPONSE_TABLE1(TLayoutDialog, TDialog)
  28.   EV_COMMAND(ID_LAYOUT, HandleLayout),
  29.   EV_LBN_SELCHANGE(ID_WINDOWLIST, HandleWindowChange),
  30. END_RESPONSE_TABLE;
  31.  
  32. TLayoutDialog::TLayoutDialog(TLayoutWindow* parent,
  33.                              TResId         resId,
  34.                              TChildInfo*    childInfo)
  35. :
  36.   TDialog(parent, resId),
  37.   LayoutWindow(parent),
  38.   ChildInfo(childInfo)
  39. {
  40.   WindowList  = new TListBox(this, ID_WINDOWLIST);
  41.  
  42.   X1EdgeCombo = new TComboBox(this,ID_X1_MYEDGE);
  43.   X2EdgeCombo = new TComboBox(this,ID_X2_MYEDGE);
  44.   Y1EdgeCombo = new TComboBox(this,ID_Y1_MYEDGE);
  45.   Y2EdgeCombo = new TComboBox(this,ID_Y2_MYEDGE);
  46.  
  47.   X1OtherEdgeCombo = new TComboBox(this,ID_X1_OTHEREDGE);
  48.   X2OtherEdgeCombo = new TComboBox(this,ID_X2_OTHEREDGE);
  49.   Y1OtherEdgeCombo = new TComboBox(this,ID_Y1_OTHEREDGE);
  50.   Y2OtherEdgeCombo = new TComboBox(this,ID_Y2_OTHEREDGE);
  51.  
  52.   X1UnitsCombo     = new TComboBox(this, ID_X1_UNITS);
  53.   X2UnitsCombo     = new TComboBox(this, ID_X2_UNITS);
  54.   Y1UnitsCombo     = new TComboBox(this, ID_Y1_UNITS);
  55.   Y2UnitsCombo     = new TComboBox(this, ID_Y2_UNITS);
  56.  
  57.   X1ValueEdit = new TEdit(this,ID_X1_VALUE);
  58.   X2ValueEdit = new TEdit(this,ID_X2_VALUE);
  59.   Y1ValueEdit = new TEdit(this,ID_Y1_VALUE);
  60.   Y2ValueEdit = new TEdit(this,ID_Y2_VALUE);
  61.  
  62.   X1RelationCombo  = new TComboBox(this, ID_X1_RELATIONSHIP);
  63.   X2RelationCombo  = new TComboBox(this, ID_X2_RELATIONSHIP);
  64.   Y1RelationCombo  = new TComboBox(this, ID_Y1_RELATIONSHIP);
  65.   Y2RelationCombo  = new TComboBox(this, ID_Y2_RELATIONSHIP);
  66.  
  67.   X1RelWinCombo    = new TComboBox(this, ID_X1_RELWIN);
  68.   X2RelWinCombo    = new TComboBox(this, ID_X2_RELWIN);
  69.   Y1RelWinCombo    = new TComboBox(this, ID_Y1_RELWIN);
  70.   Y2RelWinCombo    = new TComboBox(this, ID_Y2_RELWIN);
  71. }
  72.  
  73. struct TEnumPair {
  74.   char far* Name;
  75.   uint32    Value;
  76. };
  77. #define ENUMPAIR(enum)   {#enum, enum}
  78.  
  79.  
  80. //
  81. // For a valid constraint....
  82. // X2Edge must not be the same as X1 edge
  83. // Y2Edge must not be the same as Y1 edge
  84. // If X2Edge == lmWidth then lmLeft and lmRight relations don't make sense
  85. // if Y2Edge == lmHeight then lmAbove and lmBelow relations don't make sense
  86. //
  87.  
  88. TEnumPair X1EdgePairs[] = {
  89.   ENUMPAIR(lmLeft),
  90.   ENUMPAIR(lmCenter),
  91.   ENUMPAIR(lmRight),
  92. };
  93.  
  94. TEnumPair X2EdgePairs[] = {
  95.   ENUMPAIR(lmCenter),
  96.   ENUMPAIR(lmRight),
  97.   ENUMPAIR(lmWidth),
  98. };
  99.  
  100. TEnumPair Y1EdgePairs[] = {
  101.   ENUMPAIR(lmTop),
  102.   ENUMPAIR(lmCenter),
  103.   ENUMPAIR(lmBottom),
  104. };
  105.  
  106. TEnumPair Y2EdgePairs[] = {
  107.   ENUMPAIR(lmCenter),
  108.   ENUMPAIR(lmBottom),
  109.   ENUMPAIR(lmHeight),
  110. };
  111.  
  112. TEnumPair AllEdgePairs[] = {
  113.   ENUMPAIR(lmLeft),
  114.   ENUMPAIR(lmCenter),
  115.   ENUMPAIR(lmRight),
  116.   ENUMPAIR(lmTop),
  117.   ENUMPAIR(lmBottom),
  118.   ENUMPAIR(lmWidth),
  119.   ENUMPAIR(lmHeight),
  120. };
  121.  
  122.  
  123. // Relations for all X edges
  124. //
  125. TEnumPair XRelationPairs[] = {
  126.   ENUMPAIR(lmAsIs),
  127.   ENUMPAIR(lmAbsolute),
  128.   ENUMPAIR(lmPercentOf),
  129.   ENUMPAIR(lmSameAs),
  130.   ENUMPAIR(lmLeftOf),     // These should be disabled for lmWidth
  131.   ENUMPAIR(lmRightOf),
  132. };
  133.  
  134. TEnumPair YRelationPairs[] = {
  135.   ENUMPAIR(lmAsIs),
  136.   ENUMPAIR(lmAbsolute),
  137.   ENUMPAIR(lmPercentOf),
  138.   ENUMPAIR(lmSameAs),
  139.   ENUMPAIR(lmAbove),      // These should be disabled for lmHeight
  140.   ENUMPAIR(lmBelow),
  141. };
  142.  
  143. TEnumPair RelWinPairs[MaxWindows] = {
  144.   ENUMPAIR(lmParent),
  145. };
  146.  
  147. TEnumPair UnitsPairs[] = {
  148.   ENUMPAIR(lmPixels),
  149.   ENUMPAIR(lmLayoutUnits),
  150. };
  151.  
  152. static void
  153. FillCombo(TComboBox* combo, TEnumPair* pairs, int count)
  154. {
  155.   while (combo->DeleteString(0) > 0)
  156.     ;
  157.   for (int i = 0; i < count; i++) {
  158.     int index = combo->AddString(pairs[i].Name);
  159.     combo->SetItemData(index, pairs[i].Value);
  160.   }
  161. }
  162.  
  163. void
  164. TLayoutDialog::SetupWindow()
  165. {
  166.   TDialog::SetupWindow();
  167.  
  168.   // Fill the window list
  169.   //
  170.   int i = 0;
  171.   for (; ChildInfo[i].Child && i < MaxWindows-1; i++) {
  172.     WindowList->AddString(ChildInfo[i].Child->Title);
  173.     RelWinPairs[i+1].Name = ChildInfo[i].Child->Title;
  174.     RelWinPairs[i+1].Value = uint32(ChildInfo[i].Child);
  175.   }
  176.  
  177.   FillCombo(X1EdgeCombo,X1EdgePairs,COUNTOF(X1EdgePairs));
  178.   FillCombo(X2EdgeCombo,X2EdgePairs,COUNTOF(X2EdgePairs));
  179.   FillCombo(Y1EdgeCombo,Y1EdgePairs,COUNTOF(Y1EdgePairs));
  180.   FillCombo(Y2EdgeCombo,Y2EdgePairs,COUNTOF(Y2EdgePairs));
  181.  
  182.   FillCombo(X1OtherEdgeCombo,AllEdgePairs,COUNTOF(AllEdgePairs));
  183.   FillCombo(X2OtherEdgeCombo,AllEdgePairs,COUNTOF(AllEdgePairs));
  184.   FillCombo(Y1OtherEdgeCombo,AllEdgePairs,COUNTOF(AllEdgePairs));
  185.   FillCombo(Y2OtherEdgeCombo,AllEdgePairs,COUNTOF(AllEdgePairs));
  186.  
  187.   FillCombo(X1UnitsCombo, UnitsPairs, COUNTOF(UnitsPairs));
  188.   FillCombo(X2UnitsCombo, UnitsPairs, COUNTOF(UnitsPairs));
  189.   FillCombo(Y1UnitsCombo, UnitsPairs, COUNTOF(UnitsPairs));
  190.   FillCombo(Y2UnitsCombo, UnitsPairs, COUNTOF(UnitsPairs));
  191.  
  192.   X1ValueEdit->SetText("");
  193.   X2ValueEdit->SetText("");
  194.   Y1ValueEdit->SetText("");
  195.   Y2ValueEdit->SetText("");
  196.  
  197.   FillCombo(X1RelationCombo, XRelationPairs, COUNTOF(XRelationPairs));
  198.   FillCombo(X2RelationCombo, XRelationPairs, COUNTOF(XRelationPairs));
  199.   FillCombo(Y1RelationCombo, YRelationPairs, COUNTOF(YRelationPairs));
  200.   FillCombo(Y2RelationCombo, YRelationPairs, COUNTOF(YRelationPairs));
  201.  
  202.   FillCombo(X1RelWinCombo, RelWinPairs, i+1);
  203.   FillCombo(X2RelWinCombo, RelWinPairs, i+1);
  204.   FillCombo(Y1RelWinCombo, RelWinPairs, i+1);
  205.   FillCombo(Y2RelWinCombo, RelWinPairs, i+1);
  206.  
  207.   // This call will result in a LBN_SELCHANGE Message being sent to the
  208.   // window, which will result in HandleWindowChange being called.
  209.   // This call can't be made until the above controls are setup, so wait until
  210.   // the end here.
  211.   //
  212.   ChildNum = -1;  // Flag to signal initial call to HandleWindowChange
  213.   WindowList->SetSelIndex(0);
  214.   HandleWindowChange();
  215. }
  216.  
  217. static void
  218. SelectItemData(TComboBox* combo, uint32 itemData)
  219. {
  220.   int count = combo->GetCount();
  221.   combo->SetSelIndex(-1);  // Clear the selection
  222.   for (int i = 0; i < count; i++)
  223.     if (combo->GetItemData(i) == itemData) {
  224.       combo->SetSelIndex(i);
  225.       return;
  226.     }
  227.   TRACE("Couldn't find lmXxx (" << itemData <<") in list");
  228. }
  229.  
  230. static void
  231. SelectItemData(TComboBox* combo, TWindow* win)
  232. {
  233. #if defined(BI_DATA_NEAR)
  234.   if (OFFSETOF(win) == 0)
  235.     SelectItemData(combo, uint32(0));
  236.   else
  237. #endif
  238.     SelectItemData(combo, uint32(win));
  239. }
  240.  
  241. //
  242. // Take a given layout constraint & shove it into the dialog
  243. //
  244. void
  245. TLayoutDialog::SetLayoutConstraint(TLayoutConstraint& lc, Constraint which)
  246. {
  247.   char buff[20];
  248.  
  249.   switch (which) {
  250.     case X1:
  251.       SelectItemData(X1EdgeCombo, lc.MyEdge);
  252.       SelectItemData(X1RelationCombo, lc.Relationship);
  253.       SelectItemData(X1RelWinCombo, lc.RelWin);
  254.       SelectItemData(X1OtherEdgeCombo, lc.OtherEdge);
  255.       SelectItemData(X1UnitsCombo, lc.Units);
  256.       sprintf(buff, "%d", lc.Value);
  257.       X1ValueEdit->SetText(buff);
  258.       break;
  259.  
  260.     case X2:
  261.       SelectItemData(X2EdgeCombo, lc.MyEdge);
  262.       SelectItemData(X2RelationCombo, lc.Relationship);
  263.       SelectItemData(X2RelWinCombo, lc.RelWin);
  264.       SelectItemData(X2OtherEdgeCombo, lc.OtherEdge);
  265.       SelectItemData(X2UnitsCombo, lc.Units);
  266.       sprintf(buff, "%d", lc.Value);
  267.       X2ValueEdit->SetText(buff);
  268.       break;
  269.  
  270.     case Y1:
  271.       SelectItemData(Y1EdgeCombo, lc.MyEdge);
  272.       SelectItemData(Y1RelationCombo, lc.Relationship);
  273.       SelectItemData(Y1RelWinCombo, lc.RelWin);
  274.       SelectItemData(Y1OtherEdgeCombo, lc.OtherEdge);
  275.       SelectItemData(Y1UnitsCombo, lc.Units);
  276.       sprintf(buff, "%d", lc.Value);
  277.       Y1ValueEdit->SetText(buff);
  278.       break;
  279.  
  280.     case Y2:
  281.       SelectItemData(Y2EdgeCombo, lc.MyEdge);
  282.       SelectItemData(Y2RelationCombo, lc.Relationship);
  283.       SelectItemData(Y2RelWinCombo, lc.RelWin);
  284.       SelectItemData(Y2OtherEdgeCombo, lc.OtherEdge);
  285.       SelectItemData(Y2UnitsCombo, lc.Units);
  286.       sprintf(buff, "%d", lc.Value);
  287.       Y2ValueEdit->SetText(buff);
  288.       break;
  289.   }
  290. }
  291.  
  292. //
  293. // Retrieve a layout constraint from the dialog
  294. //
  295. void
  296. TLayoutDialog::GetLayoutConstraint(TLayoutConstraint& lc, Constraint which)
  297. {
  298.   char buff[20];
  299.   switch (which) {
  300.     case X1:
  301.       lc.MyEdge = (TEdge)X1EdgeCombo->GetItemData(X1EdgeCombo->GetSelIndex());
  302.       lc.Relationship = (TRelationship)X1RelationCombo->GetItemData(X1RelationCombo->GetSelIndex());
  303.       lc.RelWin = (TWindow*)X1RelWinCombo->GetItemData(X1RelWinCombo->GetSelIndex());
  304.       lc.OtherEdge = (TEdge)X1OtherEdgeCombo->GetItemData(X1OtherEdgeCombo->GetSelIndex());
  305.       lc.Units = (TMeasurementUnits)X1UnitsCombo->GetItemData(X1UnitsCombo->GetSelIndex());
  306.       X1ValueEdit->GetText(buff, sizeof buff);
  307.       lc.Value = atoi(buff);
  308.       break;
  309.  
  310.     case X2:
  311.       lc.MyEdge = (TEdge)X2EdgeCombo->GetItemData(X2EdgeCombo->GetSelIndex());
  312.       lc.Relationship = (TRelationship)X2RelationCombo->GetItemData(X2RelationCombo->GetSelIndex());
  313.       lc.RelWin = (TWindow*)X2RelWinCombo->GetItemData(X2RelWinCombo->GetSelIndex());
  314.       lc.OtherEdge = (TEdge)X2OtherEdgeCombo->GetItemData(X2OtherEdgeCombo->GetSelIndex());
  315.       lc.Units = (TMeasurementUnits)X2UnitsCombo->GetItemData(X2UnitsCombo->GetSelIndex());
  316.       X2ValueEdit->GetText(buff, sizeof buff);
  317.       lc.Value = atoi(buff);
  318.       break;
  319.  
  320.     case Y1:
  321.       lc.MyEdge = (TEdge)Y1EdgeCombo->GetItemData(Y1EdgeCombo->GetSelIndex());
  322.       lc.Relationship = (TRelationship)Y1RelationCombo->GetItemData(Y1RelationCombo->GetSelIndex());
  323.       lc.RelWin = (TWindow*)Y1RelWinCombo->GetItemData(Y1RelWinCombo->GetSelIndex());
  324.       lc.OtherEdge = (TEdge)Y1OtherEdgeCombo->GetItemData(Y1OtherEdgeCombo->GetSelIndex());
  325.       lc.Units = (TMeasurementUnits)Y1UnitsCombo->GetItemData(Y1UnitsCombo->GetSelIndex());
  326.       Y1ValueEdit->GetText(buff, sizeof buff);
  327.       lc.Value = atoi(buff);
  328.       break;
  329.  
  330.     case Y2:
  331.       lc.MyEdge = (TEdge)Y2EdgeCombo->GetItemData(Y2EdgeCombo->GetSelIndex());
  332.       lc.Relationship = (TRelationship)Y2RelationCombo->GetItemData(Y2RelationCombo->GetSelIndex());
  333.       lc.RelWin = (TWindow*)Y2RelWinCombo->GetItemData(Y2RelWinCombo->GetSelIndex());
  334.       lc.OtherEdge = (TEdge)Y2OtherEdgeCombo->GetItemData(Y2OtherEdgeCombo->GetSelIndex());
  335.       lc.Units = (TMeasurementUnits)Y2UnitsCombo->GetItemData(Y2UnitsCombo->GetSelIndex());
  336.       Y2ValueEdit->GetText(buff, sizeof buff);
  337.       lc.Value = atoi(buff);
  338.       break;
  339.   }
  340. }
  341.  
  342. //
  343. // Window changed
  344. // Ask user to save current layout constraints?
  345. //
  346. void TLayoutDialog::HandleWindowChange()
  347. {
  348.   if (ChildNum >= 0) {
  349.     // Save all of the current settings, unless this is the first call
  350.     //
  351.     GetLayoutConstraint(ChildInfo[ChildNum].LayoutMetrics.X,X1);
  352.     GetLayoutConstraint(ChildInfo[ChildNum].LayoutMetrics.Width,X2);
  353.     GetLayoutConstraint(ChildInfo[ChildNum].LayoutMetrics.Y,Y1);
  354.     GetLayoutConstraint(ChildInfo[ChildNum].LayoutMetrics.Height,Y2);
  355.   }
  356.  
  357.   // Switch to new window
  358.   //
  359.   ChildNum = WindowList->GetSelIndex();
  360.   if (ChildNum >= 0) {
  361.     SetLayoutConstraint(ChildInfo[ChildNum].LayoutMetrics.X,X1);
  362.     SetLayoutConstraint(ChildInfo[ChildNum].LayoutMetrics.Width,X2);
  363.     SetLayoutConstraint(ChildInfo[ChildNum].LayoutMetrics.Y,Y1);
  364.     SetLayoutConstraint(ChildInfo[ChildNum].LayoutMetrics.Height,Y2);
  365.   }
  366. }
  367.  
  368. //
  369. // Get the constraints from each window, update child window
  370. //
  371. void
  372. TLayoutDialog::HandleLayout()
  373. {
  374.   TLayoutMetrics& lm = ChildInfo[ChildNum].LayoutMetrics;
  375.  
  376.   GetLayoutConstraint(lm.X,X1);
  377.   GetLayoutConstraint(lm.Width,X2);
  378.   GetLayoutConstraint(lm.Y,Y1);
  379.   GetLayoutConstraint(lm.Height,Y2);
  380.  
  381.   // Check some basic restrictions
  382.   //
  383.   if (lm.X.MyEdge == lm.Width.MyEdge) {
  384.     MessageBox("X1.MyEdge can not be the same as X2.MyEdge");
  385.     return;
  386.   }
  387.   if (lm.Y.MyEdge == lm.Height.MyEdge) {
  388.     MessageBox("Y1.MyEdge can not be the same as Y2.MyEdge");
  389.     return;
  390.   }
  391.  
  392.   Parent->PostMessage(WM_COMMAND, CM_RELAYOUT);
  393. }
  394.